<?php  

/* GLOBAL VARIABLES */
	$firstName = "";
	$lastName = "";
	
	$TextUserDescription = "User";
	$TextRoleType = "User";	
	$IDRoleType = "2";
	
	$interfaceLanguage = "English";	
	
	$currectPage = "default";	
	
if (   isset($_GET['action'])   )
{
	$action = $_GET['action'];
	if ($action = "exit")
	{
		unset($_POST['loghin']);
		unset($_POST['password']);
		unset($_SESSION['loget']);

		/*  SET DEFAULT VALUES */
		$_SESSION['loget'] = "No";
		
		$_SESSION['FirstName'] = "";		
		$_SESSION['LastName'] = "";	
		
		$_SESSION['User_Description'] = "User";
		$_SESSION['TextRoleType'] = "User";
		$_SESSION['IDRoleType'] = "2";
		
		$_SESSION['Interface_Language'] = "English";
		
		$firstName = "";
		$lastName = "";
		
		$TextUserDescription = "User";
		$TextRoleType = "User";	
		$IDRoleType = "2";
		
		$interfaceLanguage = "English";		
	}	
} 

	
if ( (isset($_POST['loghin'])) && (isset($_POST['password'])) )
{
	$resultautentificate = mysql_query("SELECT *
											, (Select Name From Classifiers WHERE Classifiers.Code = Users.RoleType) as TextRoleType 
											, Users.RoleType as IDRoleType
											, (Select Name From Classifiers WHERE Classifiers.Code = Users.interface_language) as UserLanguage 
											FROM USERS WHERE Loghin='".$_POST['loghin']."' AND Password='".$_POST['password']."'");
											
	$autentificaterow = mysql_fetch_array($resultautentificate);
	
	if ( ($autentificaterow['UserID'] != "") )
	{
		$_SESSION['loget'] = "Yes";
		
		$_SESSION['FirstName'] = $autentificaterow['FirstName'];		
		$_SESSION['LastName'] = $autentificaterow['LastName'];	
		
		$_SESSION['User_Description'] = $autentificaterow['FirstName'].' '.$autentificaterow['LastName'];
		$_SESSION['TextRoleType'] = $autentificaterow['TextRoleType'];
		$_SESSION['IDRoleType'] = $autentificaterow['IDRoleType'];
		
		$_SESSION['Interface_Language'] = $autentificaterow['UserLanguage'];

		
		$firstName = $autentificaterow['FirstName'];
		$lastName = $autentificaterow['LastName'];	
		
		$TextUserDescription = $autentificaterow['FirstName'].' '.$autentificaterow['LastName'];
		$TextRoleType = $autentificaterow['TextRoleType'];		
		$IDRoleType = $autentificaterow['IDRoleType'];
		
		$interfaceLanguage = $autentificaterow['UserLanguage'];
	}
	else
	{
		$firstName = $_SESSION['FirstName'];
		$lastName = $_SESSION['LastName'];

		$TextUserDescription = $_SESSION['User_Description'];
		$TextRoleType = $_SESSION['TextRoleType'];	
		$IDRoleType = $_SESSION['IDRoleType'];

		$interfaceLanguage = $_SESSION['Interface_Language'];		
	}	
}

if (isset($_SESSION['FirstName'])) 			$firstName = $_SESSION['FirstName'];
if (isset($_SESSION['LastName']))  			$lastName = $_SESSION['LastName'];
if (isset($_SESSION['User_Description']))	$TextUserDescription = $_SESSION['User_Description'];
if (isset($_SESSION['TextRoleType']))		$TextRoleType = $_SESSION['TextRoleType'];	
if (isset($_SESSION['IDRoleType']))			$IDRoleType = $_SESSION['IDRoleType'];
if (isset($_SESSION['Interface_Language']))	$interfaceLanguage = $_SESSION['Interface_Language'];		


if (isset($_GET["page"]))
{
	$_SESSION['Current_Page'] = $_GET["page"];
}

if (isset($_POST["add_calendar_selected_ID"]) && isset($_POST["add_calendar_Age"])   )
{
	$user_id_to_add_new_period = $_POST["add_calendar_selected_ID"];
	$add_calendar_Age = $_POST["add_calendar_Age"];
	$owerWriteOldRec = $_POST["add_calendar_owerwrite_Old_Records"];
	
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '1', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '2', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '3', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '4', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '5', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '6', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '7', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '8', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '9', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '10', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '11', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	$add_period_age_result = mysql_query("INSERT INTO CALENDAR (age, month, user_ID)  VALUES ('".$add_calendar_Age."', '12', '".$user_id_to_add_new_period."')       "); if (!$add_period_age_result) {    die('Invalid query: ' . mysql_error());  }
	
}

if (isset($_POST["confirm_edit_calendar_Age"]) 
		&&  isset($_POST["confirm_edit_calendar_month"]) 
		&&  isset($_POST["confirm_edit_statusPayment"]) 
		&&  isset($_POST["confirm_edit_paymentDate"]) 
		&&  isset($_POST["confirm_edit_summPaymnet"]) 
		&& isset($_POST["confirm_edit_messagePaymnet"]) 
		&& isset($_POST["confirm_edit_confirmed_Payment"])
		&& isset($_POST["confirm_edit_userID"]))
{
	$confirm_edit_calendar_Age = trim($_POST["confirm_edit_calendar_Age"]);
	$confirm_edit_calendar_month = trim($_POST["confirm_edit_calendar_month"]);
	$confirm_edit_statusPayment = trim($_POST["confirm_edit_statusPayment"]);
	$confirm_edit_paymentDate = trim($_POST["confirm_edit_paymentDate"]);
	$confirm_edit_summPaymnet = trim($_POST["confirm_edit_summPaymnet"]);
	$confirm_edit_messagePaymnet = trim($_POST["confirm_edit_messagePaymnet"]);
	$confirm_edit_confirmed_Payment = trim($_POST["confirm_edit_confirmed_Payment"]);
	$confirm_edit_userID = trim($_POST["confirm_edit_userID"]);
		
	$confirm_editcalendar_result = mysql_query(" UPDATE CALENDAR SET data_payment='$confirm_edit_paymentDate' , status_pay = $confirm_edit_statusPayment, summa = $confirm_edit_summPaymnet, confirmation = $confirm_edit_confirmed_Payment, message = '$confirm_edit_messagePaymnet' WHERE age = $confirm_edit_calendar_Age AND month = $confirm_edit_calendar_month  AND user_ID = $confirm_edit_userID"); if (!$confirm_editcalendar_result) {    die('Invalid query: ' . mysql_error());  }	
	
	$_SESSION['Current_Page'] = "calendar";
}

if (isset($_GET["deleteUser"]))
{	
	$deleteUserID = $_GET["deleteUser"];
	$deteleUser_result = mysql_query(" Delete From Users Where UserID = $deleteUserID"); if (!$deteleUser_result) {    die('Invalid query: ' . mysql_error());  }	
}

if ( isset($_POST["add_user_FirstName"]) 
		&& isset($_POST["add_user_LastName"]) 		
		&& isset($_POST["add_user_Loghin"]) 		
		&& isset($_POST["add_user_Password"]) 		
		&& isset($_POST["addUser_RoleType"]) 		
		&& isset($_POST["add_user_PasswordStatus"]) 		
		&& isset($_POST["add_user_RecordStatus"]) 
		&& isset($_POST["add_user_InterfaceLanguage"]))
{
		$add_user_FirstName = $_POST["add_user_FirstName"];
		$add_user_LastName = $_POST["add_user_LastName"];
		$add_user_Loghin = $_POST["add_user_Loghin"];
		$add_user_Password = $_POST["add_user_Password"];
		$addUser_RoleType = $_POST["addUser_RoleType"];
		$add_user_PasswordStatus = $_POST["add_user_PasswordStatus"];
		$add_user_RecordStatus = $_POST["add_user_RecordStatus"];
		$add_user_InterfaceLanguage = $_POST["add_user_InterfaceLanguage"];
		$today = date("m/d/y");    
		
		$insertQuery = "INSERT INTO USERS (FirstName, LastName, Loghin, Password ,RoleType ,PasswordStatus ,RecordStatus, EditUserID, EditDate, interface_language)";
		$insertQuery .= " VALUES ('$add_user_FirstName', '$add_user_LastName', '$add_user_Loghin', '$add_user_Password', $addUser_RoleType, $add_user_PasswordStatus, $add_user_RecordStatus, 0,  $today, $add_user_InterfaceLanguage)";
		
		
		$add_newUser_result = mysql_query($insertQuery); if (!$add_newUser_result) {    die('Invalid query: ' . mysql_error());  }	
	
	
}




?>